package com.lm.Prototype;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Prototype implements Cloneable{
    private List<String> hobby;
    public void setHobby(List<String> hobby) {this.hobby = hobby;}
    public List<String> getHobby() {return hobby;}
    public Prototype() {}

    public Object clone() throws CloneNotSupportedException {
        Prototype clone = (Prototype) super.clone();
        //新建一个对象再重新赋值给克隆对象
        List<String> newHobby = new ArrayList<>();
        newHobby.addAll(clone.getHobby());
        clone.setHobby(newHobby);
        return clone;
    }

    public static void main(String[] args) throws CloneNotSupportedException {
        //创建原型对象
        Prototype prototype = new Prototype();
        prototype.setHobby(Arrays.asList("篮球", "读书", "看美女"));
        //基于原型对象创建新对象
        Prototype clone = (Prototype) prototype.clone();
        System.out.println("prototype == clone:" + (prototype == clone));
        clone.getHobby().add("游戏");
        System.out.println("prototype.hobby == clone.hobby:" + (prototype.getHobby() == clone.getHobby()));
    }
}
